﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using BusinessObjects;
using DataAccess;
using DCMSExceptions;
using System.Data;

namespace BusinessLogic {
  /// <summary>
  /// Business Logic Layer for trustpersons
  /// </summary>
  public class TrustPersonBll : ITrustPersonLogic {

    private TrustPersonDal dal = new TrustPersonDal( );
    private StringBuilder errorBuilder = new StringBuilder( );

    #region - Public methods -

    /// <summary>
    /// Gets the trust persons for child.
    /// </summary>
    /// <param name="childId">The child id.</param>
    /// <returns></returns>
    public TrustPersons GetTrustPersonsForChild( int childId ) {
      TrustPersons persons;
      try {
        persons = dal.GetTrustPersonsForChild( childId );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij ophalen van vertrouwenspersoon voor een kind.", ex );
      }
      return persons;
    }

    /// <summary>
    /// Deletes the trust person.
    /// </summary>
    /// <param name="trustPerson">The trust person.</param>
    public void DeleteTrustPerson( TrustPerson trustPerson ) {
      try {
        dal.DeleteTrustPerson( trustPerson );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij verwijderen van vertrouwenspersoon.", ex );
      }

    }

    /// <summary>
    /// Saves the trust person.
    /// </summary>
    /// <param name="trustPerson">The trust person.</param>
    public void SaveTrustPerson( TrustPerson trustPerson ) {
      if ( Validate( trustPerson ) ) {
        try {
          dal.SaveTrustPerson( trustPerson );
        } catch ( DataException ex ) {
          throw new BusinessException( "Fout bij bewaren van vertrouwenspersoon", ex );
        }
      } else {
        throw new BusinessException( trustPerson.BrokenRulesMessages );
      }

    }

    #endregion

    private bool Validate( TrustPerson trustPerson ) {
      bool valid = true;

      if ( string.IsNullOrEmpty( trustPerson.Name ) ) {
        valid = false;
        errorBuilder.Append( "Vertrouwenspersoon naam niet ingevuld.\n" );
      }

      return valid;
    }

  }
}
